package ae.pcfc.cldos.online.web.controller.client.company.ev.termination;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import ae.pcfc.cldos.online.common.ConstantsCommon.Steps;
import ae.pcfc.cldos.online.common.clddb.domain.AttachmentType;
import ae.pcfc.cldos.online.common.delegate.EmploymentVisaCancelationDelegate;
import ae.pcfc.cldos.online.common.vo.FileVO;
import ae.pcfc.cldos.online.common.vo.ev.ren.can.EmploymentVisaCancelationVO;
import ae.pcfc.cldos.online.common.vo.ev.ren.can.SubmitExitPaperVO;
import ae.pcfc.cldos.online.web.ViewPath;
import ae.pcfc.cldos.online.web.controller.base.BasePageController;
import ae.pcfc.cldos.online.web.model.EVTerminationModel;
import ae.pcfc.cldos.online.web.model.SubmitExitPaperModel;

/**
 * 
 * @author arosa
 *
 */
@Controller
@RequestMapping(value = ViewPath.__ADMIN_EMPLOYMENT_VISA_TERMINATE_EXIT_PAPER)
public class EVTerminationExitPaperSubmitController extends BasePageController {

	private static final String VIEW_PATH = "client.company.evterminate.exitpaper.form";
	
	@Autowired
	@Qualifier("employmentVisaCancelationDelegate")
	private EmploymentVisaCancelationDelegate employmentVisaCancelationDelegate;
	
	@ModelAttribute("evTerminationModel")
	public EVTerminationModel populateRegisterEmploymentVisaReviewerInfoModel(@RequestParam String appId,
			HttpServletRequest request, ModelMap model) throws Exception {
		EVTerminationModel evTerminationModel = new EVTerminationModel();
		
		EmploymentVisaCancelationVO employmentVisaCancelationVO =
				employmentVisaCancelationDelegate.getEmploymentVisaCancelationAppInfo(
						encryptionDelegate.decodeAndDecrypt(appId), getUserVO(), Steps.EXIT_PAPER_SUBMIT);
		
		mapper.map(employmentVisaCancelationVO.getEmploymentVisaCancelationDetailsVO(), evTerminationModel);
		model.put("appId", appId);
		return evTerminationModel;
	}
	
	@RequestMapping(value = "/form.htm", method = RequestMethod.GET)
	public ModelAndView form() {
		return new ModelAndView(VIEW_PATH);
	}
	
	@RequestMapping(value = "/form.htm", method = RequestMethod.POST)
	public ModelAndView onSubmit(@ModelAttribute(value="submitExitPaperModel") SubmitExitPaperModel submitExitPaperModel,
			BindingResult errors) throws Exception {
		ModelAndView mav = null;
		
		if(!errors.hasErrors()){
			SubmitExitPaperVO submitExitPaperVO = new SubmitExitPaperVO();
			submitExitPaperVO.setAppId(encryptionDelegate.decodeAndDecrypt(submitExitPaperModel.getAppId()));
			submitExitPaperVO.setUserVO(getUserVO());
			FileVO fileVO = createFileVOFromMultipartFile(
					submitExitPaperModel.getExitPaper(), AttachmentType.__ATTACHMENT_TYPE_EXIT_DOCUMENT);
			submitExitPaperVO.setExitPaper(fileVO);
			
			employmentVisaCancelationDelegate.submitExitDocument(submitExitPaperVO);
			mav = new ModelAndView(ViewPath.__REDIRECT+ViewPath.__ADMIN_TASKS+"/show.htm");
		} else {
			mav = new ModelAndView(VIEW_PATH);
		}
		
		return mav; 
	}
}
